﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ServiceFault.cs" company="Spenta Consulting">
//   (c) Copyright 2011
// </copyright>
// <summary>
//   Service fault (Contract fault wrapped to WCF-Silverlight transport)
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Spenta.StreetCare.MVC.Services.Data
{
    #region Using Directives

    using System;
    using System.Runtime.Serialization;

    #endregion

    /// <summary>
    /// Service fault (Contract fault wrapped to WCF-Silverlight transport)
    /// </summary>
    [DataContract]
    public class ServiceFault
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceFault"/> class.
        /// </summary>
        /// <param name="ex">The exception.</param>
        /// <param name="reason">The reason.</param>
        /// <param name="code">The fault code.</param>
        private ServiceFault(Exception ex, string reason, uint code)
        {
            this.Reason = reason;
            this.Code = code;
            this.Exception = ex != null ? new ServiceException(ex) : null;
        }

        /// <summary>
        /// Gets or sets the exception.
        /// </summary>
        /// <value>The exception.</value>
        [DataMember]
        public ServiceException Exception { get; set; }

        /// <summary>
        /// Gets or sets the message.
        /// </summary>
        /// <value>The message.</value>
        [DataMember]
        public string Reason { get; set; }

        /// <summary>
        /// Gets or sets the code.
        /// </summary>
        /// <value>The fault code.</value>
        [DataMember]
        public uint Code { get; set; }

        /// <summary>
        /// Creates a service fault.
        /// </summary>
        /// <returns>A Service Fault</returns>
        public static ServiceFault Create()
        {
            return Create(null, string.Empty, 0);
        }

        /// <summary>
        /// Creates a service fault.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="code">The fault code.</param>
        /// <returns>A Service Fault</returns>
        public static ServiceFault Create(string message, uint code)
        {
            return Create(null, message, code);
        }

        /// <summary>
        /// Creates a service fault.
        /// </summary>
        /// <param name="ex">The exception.</param>
        /// <param name="code">The fault code.</param>
        /// <returns>A Service Fault</returns>
        public static ServiceFault Create(Exception ex, uint code)
        {
            return Create(ex, string.Empty, code);
        }

        /// <summary>
        /// Creates a service fault.
        /// </summary>
        /// <param name="ex">The exception.</param>
        /// <param name="message">The message.</param>
        /// <param name="code">The fault code.</param>
        /// <returns>A service fault</returns>
        public static ServiceFault Create(Exception ex, string message, uint code)
        {
            return new ServiceFault(ex, message, code);
        }
    }
}